package mina;

import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;

import util.Security;

import dao.Message;
import dao.MessageDAO;

public class Processor {
	private static JSONParser parser = new JSONParser();
	MessageDAO dao =  null;
	{
		dao = new MessageDAO("c:\\DB","MESSAGEDB");
	}
	/**
	 * 
	 * to server : {"cmd":"send_message","from":"ADSFASDFSDFAS","to":"ADSFASDFSDFAS", "body":"Hi i love you"}
	 * from server : {"cmd":"send_message_result","code":"ok"}
	 * to server : {"cmd":"get_message","to":"ADSFASDFSDFAS"}
	 * from server : {"cmd":"get_message_result","from":"ADSFASDFSDFAS","to":"ADSFASDFSDFAS", "body":"Hi i love you"}
	 * @param message
	 * @return
	 */
	public Object command(Object message){
		String rsp = (String)message;
		System.out.println(rsp);
		JSONObject root = null;
		try {
			root = (JSONObject) parser.parse(rsp);
		} catch (Exception e) {
			e.printStackTrace();
		}
		String cmd = (String)root.get("cmd");
		System.out.println("cmd is " + cmd);
		if("send_message".equals(cmd)){
			return processSendMessage(root);
		}else if("get_message".equals(cmd)){
			return processGetMessage(root);
		}
		return null;
	}

	private Object processGetMessage(JSONObject root) {
		System.out.println("processGetMessage");
		JSONObject result = new JSONObject();
		List<Message> ret = null;
		List<Message> delList = new ArrayList<Message>();
		try{
			String to = (String)root.get("to");
			String key = Security.makeMD5(to);
			ret = dao.get(Security.makeMD5(to));
			System.out.println( "result size is " + ret.size() );
			for(Message p1 : ret){
				System.out.println(p1.getTo() +":"+ p1.getFrom() + ":" + p1.getBody());
				if(!p1.isCheck()){
					result.put("cmd","get_message_result");
					result.put("from", p1.getFrom());
					result.put("to", p1.getTo());
					result.put("body", p1.getBody());
					
					delList.add(new Message(key,key,"",true));
				}
			}
			// delete message after getMessage 
			for(Message k:delList){
				boolean ff = dao.delete(k);
				System.out.println(k.getTo()+ " delete result " + ff);
				
			}
			
			
		}catch(Exception e){
			e.printStackTrace();
		}
		
		return result.toJSONString();
	}
	
	private String processSendMessage(JSONObject root) {
		System.out.println("processSendMessage");
		String resultStr = "";
		try {
			String from = (String)root.get("from");
			String to = (String)root.get("to");
			String body = (String)root.get("body");

			dao.Message m = new dao.Message();
			m.setTo(Security.makeMD5(to));
			m.setFrom(Security.makeMD5(from));
			m.setBody(body);		
			m.setCheck(false);
			List<Message> pa = new ArrayList<Message>();
			pa.add(m);
			boolean chk = dao.set(pa);
			System.out.println("dd   " + chk);
			if(chk){
				JSONObject result = new JSONObject();
				result.put("cmd", "send_message_result");
				result.put("code","ok");
				resultStr =  result.toJSONString();
				System.out.println(resultStr);
			}
		} catch (Exception e2) {
			e2.printStackTrace();
		}
		return resultStr;
	}
}
